import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Injectable()
export class CacheRedisService {
  constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}

  async get<T = any>(id: string): Promise<T> {
    return await this.cacheManager.get(id);
  }
  /**
   *
   * @param key 键
   * @param value 值
   * @param ttl 生存时间(秒) 默认半小时
   *
   */
  async set<T>(key: string, value: T, ttl = 1800) {
    return await this.cacheManager.set(key, value, { ttl });
  }

  /**
   *
   * @param key redis中键
   */
  async del(key: string) {
    await this.cacheManager.del(key);
  }
}
